import LXCOM from '../../common/common.js'
import PolygonGraph from '../polygon.js'
import { CoordTRFUtil } from '../../common/tools/coordtool.js'
import utilfuncs from '../../common/tools/utilfuncs.js'
var funcs = utilfuncs.UtilFuncs
import { FillUtil } from '../../graph/paintutil/fillutil.js'
import { Strokeutil } from '../../graph/paintutil/strokeutil.js'

import { LGXEnums } from '../../common/sysenum.js'
import { GlobalVar } from '../../common/globalvar.js'


import polygon from '../../common/struct/basestruct/regularpolygon.js'
//GlobalConst.BaseUnitPolygon = 0x030d01xx;//后两位表示角数
class RegularPolygonGraph extends PolygonGraph.PolygonGraph {
    constructor(type) {
        super(type);
        this.m_graphType = LGXEnums.LGXGraphType.RegularPolygon_type;
        this.centerPt = new LXCOM.LGXPoint();
        this.width = 120;
        this.height = 120;
        this.edgeCount = this._type & 0x000000ff;
        this._fillDef.fillStyle = LGXEnums.LGXFillStyle.SOLID;
        this._fillDef.fillColor.clone(GlobalVar.visoDefaultFillColor);
    }

    display(map, mapInfo, forClip) {
        var scaling = mapInfo.getScaling();
        map.save();
        map.beginPath();
        var position = this.getPosCopy();
        var lenth = position.posList.length;
        var pt = position.posList[0];
        var p0 = CoordTRFUtil.world2Screen(pt, mapInfo);
        map.moveTo(p0.x, p0.y);
        for (var i = 1; i < lenth; i++) {
            pt = position.posList[i];
            var p = CoordTRFUtil.world2Screen(pt, mapInfo);
            map.lineTo(p.x, p.y);
        }
        map.closePath();

        if (!forClip) {
            Strokeutil.active(this._lineDef, this, map, mapInfo);
            FillUtil.active(this._fillDef, map, this, mapInfo, false);

            super.displayGraphScope(map, mapInfo);
            this.displayFeaturePoint(map, mapInfo);
        }
        map.restore();
    }

    disInSymbol(transformInfo, map, mapInfo, forClip) {
        var scaling = mapInfo.getScaling();
        map.save();
        map.beginPath();
        var position = this.getPosCopy();
        var lenth = position.posList.length;
        var pt = position.posList[0];
        var wpt = CoordTRFUtil.transLocalPt2World(pt, 0, transformInfo);
        let p0 = CoordTRFUtil.world2Screen(wpt, mapInfo);
        map.moveTo(p0.x, p0.y);
        for (var i = 1; i < lenth; i++) {
            pt = position.posList[i];
            wpt = CoordTRFUtil.transLocalPt2World(pt, 0, transformInfo);
            let p = CoordTRFUtil.world2Screen(wpt, mapInfo);
            map.lineTo(p.x, p.y);
        }
        map.closePath();

        if (!forClip) {
            Strokeutil.active(this._lineDef, this, map, mapInfo);
            FillUtil.active(this._fillDef, map, this, mapInfo, true, transformInfo);
        }
        map.restore();
    }

    calScope(mapscale) {
        var position = this.getPos();
        if (position.posList.length == 0){
            this.initData(mapscale);
        }
        super.calScope(mapscale);
    }

    initData(mapscale) {
        let xRadiusOuter = this.width / 2;
        let yRadiusOuter = this.height / 2;

        let stDgree = this.calStartDegree();
        let rotAngleDegree = this.getAngle();
        stDgree += rotAngleDegree;
        var position = this.getPos();
        position.clear();
        let angleSpan = 360 / this.edgeCount;
        for (let i = 0; i < this.edgeCount; i++) {
            let degree = stDgree + i * angleSpan;
            let radVal = funcs.degree2Radian(degree);
            let x = this.centerPt.x + xRadiusOuter * Math.cos(radVal);
            let y = this.centerPt.y + yRadiusOuter * Math.sin(radVal);
            position.addPoint({ x, y });
        }
        this.calScope(mapscale);
    }

    getLocalZone() {
        
        let mtx = this.getLocalMatrix();
        let inverseMtx = mtx.Inverse();

        let position = this.getPosCopy();
        let localPtList = [];
        position.posList.forEach(pt => {
            let localPt = inverseMtx.MultT({x:pt.x,y:pt.y,z:0});
            localPtList.push(localPt);
        });

        let rect = funcs.calPtListZone(localPtList);
        return rect;
    }

    calStartDegree() {
        let ptList = [];
        let xRadiusOuter = this.width / 2;
        let yRadiusOuter = this.height / 2;
        let angleSpan = 360 / this.edgeCount;
        for (let i = 0; i < this.edgeCount; i++) {
            let degree = i * angleSpan;
            let radVal = funcs.degree2Radian(degree);
            let x = xRadiusOuter * Math.cos(radVal);
            let y = yRadiusOuter * Math.sin(radVal);
            ptList.push({ x, y });
        }

        let minyPt = ptList[0];
        let minyIndex = 0;
        let miny = minyPt.y;
        for (let i = 1; i < ptList.length; i++) {
            let pt = ptList[i];
            if (miny > pt.y) {
                miny = pt.y;
                minyIndex = i;
                minyPt = pt;
            }
        }

        let nextIndex = minyIndex + 1;
        if (minyIndex == ptList.length - 1) {
            nextIndex = 0;
        }
        let nextPt = ptList[nextIndex];
        let vector = funcs.getVector(minyPt, nextPt);
        let degree = vector.angleFrmXAxisPositiveDir();
        return -degree;
    }


    displayFeaturePoint(map, mapInfo) {
        if (!this._selected || mapInfo.paintForGenerateICON) {
            return;
        }
        var ptlst = this.getScopeVertexPtLst(mapInfo.getScaling());
        this.displayDashLineRectScope(ptlst, map, mapInfo)
    }

    calculateSubKind_test(tPoint, scaling, mapscale) {
        var position = this.getPosCopy();
        var lenth = position.posList.length;
        if (lenth < 2) {
            return 0;
        }
        var seleKind = 0;
        var tAbsorbPt = new LXCOM.LGXPoint();
        tAbsorbPt.clone(tPoint);

        //多边形头尾是相连的
        var pPrePos = position.posList[0];
        let posList = position.posList;
        posList.push(pPrePos);

        let minDis = 100;
        var cmpGap = super.getSelCmpGap(scaling, mapscale);
        for (var i = 1; i < lenth; i++) {
            let pCntPos = posList[i];
            let dis = funcs.distanceLineSec4Visual(tPoint, pPrePos, pCntPos, scaling);
            if (dis <= cmpGap && dis >= 0 && dis <= minDis) {
                minDis = dis;
                tAbsorbPt.clone(tPoint);
                seleKind = lenth + 1;
            }
            pPrePos = pCntPos;
        }

        if (seleKind > 0) {
            tPoint.clone(tAbsorbPt);
        }

        return seleKind;
    }

    movePosOffset(tOffset, modCmdInfo) {
        if (this._selKind < 1) {
            return;
        }
        var position = this.getPos();
        var lenth = position.posList.length;
        if (this._selKind > lenth) {
            this.translate(tOffset, modCmdInfo.mapscale, false);
        }
    }

    translate(tOffset, mapscale, multiTransFlag, multiSelLineList) {  
        // console.log("tOffset:");
        // console.log(tOffset);      
        let preCnt = this.getLocalOrigin();
        this._position.offset(tOffset.x,tOffset.y);
        this.calScope(mapscale);
        this.translateGradientFill(tOffset);
        this.centerPt.x += tOffset.x;
        this.centerPt.y += tOffset.y;
        this.translateAttachInfo(preCnt, tOffset, mapscale, multiTransFlag, multiSelLineList);
        return 0;
    }

    assignInfo(info) {
        super.assignInfo(info);
        this.centerPt.x = info.x;
        this.centerPt.y = info.y;
        this.width = info.width;
        this.height = info.height;
        this.edgeCount = info.edgeCount;
        if (!this.edgeCount) {
            this.edgeCount = 3;
        }
    }

    modifyAttribute(info, mapscale) {
        super.modifyCommonAttribute(info);
        this.centerPt.x = info.x;
        this.centerPt.y = info.y;
        this.width = info.width;
        this.height = info.height;
        this.edgeCount = info.edgeCount;
        if (!this.edgeCount) {
            this.edgeCount = 3;
        }
        this.calScope(mapscale);
    }

    toInfo() {
        let info = new polygon.RegulaPolygonUnit();
        super.toInfo4InheritClass(info);
        info.x = this.centerPt.x;
        info.y = this.centerPt.y;
        info.width = this.width;
        info.height = this.height;
        info.edgeCount = this.edgeCount;
        return info;
    }

    rotateCoord(rotCnt, radAngle) {
        let scnt = funcs.rotate(this.centerPt, rotCnt, radAngle);
        this.centerPt.clone(scnt);
        super.rotateCoord(rotCnt, radAngle);
    }

    getMainFeaturePTLst(tFeaturePTLst,transformInfo) {
        let featurePT = this.convertPt2World(this.centerPt,transformInfo);
        tFeaturePTLst.push({ pt: featurePT, kind: LGXEnums.AuxPointKind.end });
        super.getMainFeaturePTLst(tFeaturePTLst);
    }
    act2Mouse(mosEvnt, worldPos) {
        var ret = LGXEnums.LGXPlotState.NoneState;
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (this.getStart() == 0) {
                this.setStart(1);
                this.setFinish(1);
                this.centerPt.x = worldPos.x;
                this.centerPt.y = worldPos.y;
                ret = LGXEnums.LGXPlotState.FinistState;
                this.mouseDownTime = 1;
            }
        }
        this.calScope(1);
        this.notifyToObserver();
        return ret;
    }

    getLocalOrigin() {
        let pt = { x: 0, y: 0 };
        pt.x = this.centerPt.x;
        pt.y = this.centerPt.y;
        return pt;
    }

    //复制粘贴时，从复制源对象复制属性到本图形对应属性----虚函数，由实体继承类实现
    copySubPropFromBuf(info,tx,ty){
        this.width = info.width;
        this.height = info.height;
        this.centerPt.x = info.x + tx;
        this.centerPt.y = info.y + ty;
        this.copyCommnonPropFromBuf(info,tx,ty);
    }
}


export default { RegularPolygonGraph }
